<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
            "http://www.w3.org/TR/REC-html40/loose.dtd">
<HTML>
<HEAD>



<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<META name="GENERATOR" content="hevea 1.08">
<LINK rel="stylesheet" type="text/css" href="tutorial.css">
<TITLE>
Real number basics
</TITLE>
</HEAD>
<BODY >
<A HREF="tutorial063.html"><IMG SRC ="contents_motif.gif" ALT="Up"></A>
<A HREF="tutorial065.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
<HR>

<H2 CLASS="section"><A NAME="htoc135">9.1</A>&nbsp;&nbsp;Real number basics</H2>
In general, real values cannot be represented exactly if the representation
is explicit. As a result, they are usually approximated on computers by
floating point numbers, which have a finite precision. This approximation
is sufficient for most purposes; however, in some situations it can lead to
significant error. Worse, there is usually nothing to indicate that the
final result has significant error; this can lead to completely wrong
answers being accepted as correct.<BR>
<BR>
One way to deal with this is to use <EM>interval arithmetic</EM>.
 <A NAME="@default225"></A>The basic idea is that rather than using a single floating point value to
approximate the true real value, a pair of floating point bounds are used
which are guaranteed to enclose the true real value. Each arithmetic
operation is performed on the interval represented by these bounds, and the
result rounded to ensure it encloses the true result. The result is that
any uncertainty in the final result is made explicit: while the true real
value of the result is still not known exactly, it is guaranteed to lie
somewhere in the computed interval.<BR>
<BR>
Of course, interval arithmetic is no panacea: it may be that the final
interval is too wide to be useful. However this indicates that the problem
was probably ill-conditioned or poorly computed: if the same computation had
been performed with normal floating point numbers, the final floating point
value would probably not have been near the true real value, and there would
have been no indication that there might be a problem.<BR>
<BR>
<A NAME="@default226"></A>
In ECL<SUP><I>i</I></SUP>PS<SUP><I>e</I></SUP>, such intervals are represented using the <EM>bounded real</EM>
data type.<BR>
<BR>

	<BLOCKQUOTE CLASS="figure"><DIV CLASS="center"><HR WIDTH="80%" SIZE=2></DIV>
	<DIV CLASS="center">
	<TABLE CELLPADDING=10>
<TR><TD BGCOLOR="#DB9370">
	
<UL CLASS="itemize"><LI CLASS="li-itemize">
Bounded reals are written as two floating point bounds separated 
	by a double underscore (e.g. <TT>1.5__2.0</TT>, <TT>1.0__1.0</TT>,
	<TT>3.1415926535897927__3.1415926535897936</TT>)
<LI CLASS="li-itemize">Other numeric types can be converted to bounded reals by giving them
	a <TT>breal/1</TT> wrapper, or by calling
	<A HREF="../bips/kernel/arithmetic/breal-2.html"><B>breal/2</B></A><A NAME="@default227"></A> directly
<LI CLASS="li-itemize">Bounded reals are not usually entered directly by the user; normally
	they just occur as the results of computations
<LI CLASS="li-itemize">A bounded real represents a single real number whose value is
	known to lie somewhere between the bounds and is uncertain only
	because of the limited precision with which is has been calculated
<LI CLASS="li-itemize">An arithmetic operation is only performed using bounded reals if at
	least one of its arguments is a bounded real
</UL>

	</TD>
</TR></TABLE>
	</DIV>
	<BR>
<BR>
<DIV CLASS="center">Figure 9.1: Bounded reals</DIV><BR>
<BR>

	<DIV CLASS="center"><HR WIDTH="80%" SIZE=2></DIV></BLOCKQUOTE>
An example of using bounded reals to safely compute the square root of 2:
<BLOCKQUOTE CLASS="quote"><PRE CLASS="verbatim">
?- X is sqrt(breal(2)).
X = 1.4142135623730949__1.4142135623730954
Yes
</PRE></BLOCKQUOTE>
To see how using ordinary floating point numbers can lead to inaccuracy, try
dividing 1 by 10, and then adding it together 10 times. Using floats the
result is not 1.0; using bounded reals the computed interval contains 1.0
and gives an indication of how much potential error there is:
<BLOCKQUOTE CLASS="quote"><PRE CLASS="verbatim">
?- Y is float(1) / 10, X is Y + Y + Y + Y + Y + Y + Y + Y + Y + Y.
X = 0.99999999999999989
Y = 0.1
Yes
?- Y is breal(1) / 10, X is Y + Y + Y + Y + Y + Y + Y + Y + Y + Y.
X = 0.99999999999999978__1.0000000000000007
Y = 0.099999999999999992__0.1
Yes
</PRE></BLOCKQUOTE>
<HR>
<A HREF="tutorial063.html"><IMG SRC ="contents_motif.gif" ALT="Up"></A>
<A HREF="tutorial065.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
</BODY>
</HTML>
